/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.saleorder.service;

import java.math.BigDecimal;
import java.util.List;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.SnowFlake;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.mapper.AccountMapper;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.freezingassets.entity.FreezingAssets;
import com.jeeplus.modules.freezingassets.mapper.FreezingAssetsMapper;
import com.jeeplus.modules.freezingassets.service.FreezingAssetsService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.positionassets.entity.PositionAssets;
import com.jeeplus.modules.positionassets.mapper.PositionAssetsMapper;
import com.jeeplus.modules.positionassets.service.PositionAssetsService;
import com.jeeplus.modules.purchaseorder.entity.PurchaseOrder;
import com.jeeplus.modules.purchaseorder.mapper.PurchaseOrderMapper;
import com.jeeplus.modules.purchaseorder.service.PurchaseOrderService;
import com.jeeplus.modules.subgoods.entity.SubGoods;
import com.jeeplus.modules.subgoods.service.SubGoodsService;
import com.jeeplus.modules.teamservice.service.TeamServiceService;
import com.jeeplus.modules.traderecord.entity.TradeRecord;
import com.jeeplus.modules.traderecord.mapper.TradeRecordMapper;
import com.jeeplus.modules.traderecord.service.TradeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.saleorder.entity.SaleOrder;
import com.jeeplus.modules.saleorder.mapper.SaleOrderMapper;

/**
 * 卖出订单Service
 * @author w
 * @version 2021-03-27
 */
@Service
@Transactional(readOnly = true)
public class SaleOrderService extends CrudService<SaleOrderMapper, SaleOrder> {
	@Autowired
	private SubGoodsService subGoodsService;
	@Autowired
	private PurchaseOrderService purchaseOrderService;
	@Autowired
	private PurchaseOrderMapper purchaseOrderMapper;
	@Autowired
	private AccountService accountService;
	@Autowired
	private AccountMapper accountMapper;
	@Autowired
	private MemberService memberService;
	@Autowired
	private TradeRecordService tradeRecordService;
	@Autowired
	private TradeRecordMapper tradeRecordMapper;
	@Autowired
	private PositionAssetsService positionAssetsService;
	@Autowired
	private PositionAssetsMapper positionAssetsMapper;
	@Autowired
	private FreezingAssetsService freezingAssetsService;
	@Autowired
	private FreezingAssetsMapper freezingAssetsMapper;
	@Autowired
	private TeamServiceService teamServiceService;

	public SaleOrder get(String id) {
		return super.get(id);
	}
	
	public List<SaleOrder> findList(SaleOrder saleOrder) {
		return super.findList(saleOrder);
	}
	
	public Page<SaleOrder> findPage(Page<SaleOrder> page, SaleOrder saleOrder) {
		return super.findPage(page, saleOrder);
	}
	
	@Transactional(readOnly = false)
	public void save(SaleOrder saleOrder) {
		super.save(saleOrder);
	}
	
	@Transactional(readOnly = false)
	public void delete(SaleOrder saleOrder) {
		super.delete(saleOrder);
	}

	public List<SaleOrder> findStandardSaleOrders(SaleOrder saleOrder) {
		return mapper.findStandardSaleList(saleOrder);
	}

	/**
	 * 卖出
	 * @param goodsId
	 * @param member
	 * @param price
	 * @param num
	 * @param charge
	 * @param totalPrice
	 * @return
	 */
	@Transactional(readOnly = false)
	public synchronized AjaxJson saleGoods(String goodsId, Member member, double price, int num, double charge, double totalPrice) {
		AjaxJson aj = new AjaxJson();
		SubGoods subGoods = subGoodsService.get(goodsId);
		if (subGoods != null){
			if (!"1".equals(subGoods.getIsTrade())){
				aj.setSuccess(false);
				aj.setMsg("该商品暂不可交易");
				return aj;
			}
			PositionAssets p1 = new PositionAssets();
			p1.setMember(member);
			p1.setSubGoods(subGoods);
			p1.setIsApply("0");
			List<PositionAssets> list1 = positionAssetsService.findList(p1);
			//查询已挂待交易的买入订单
			SaleOrder s1 = new SaleOrder();
			s1.setMember(member);
			s1.setIsRevoke("0");
			s1.setIsDeal("0");
			s1.setSubGoods(subGoods);
			List<SaleOrder> slist = findList(s1);
			int saleNum = 0;
			if (slist.size()>0){
				saleNum = list1.get(0).getGoodsNum()-slist.stream().mapToInt(SaleOrder::getNonNum).sum();
			}else {
				if (list1.size()>0){
					saleNum=list1.get(0).getGoodsNum();
				}
			}
			if (num > saleNum){
				aj.setSuccess(false);
				aj.setMsg("卖出数量不足");
				return aj;
			}
//			TradeRecord t = new TradeRecord();
//			t.setSubGoods(subGoods);
			List<TradeRecord> tlist = tradeRecordService.findYesterdayBeforeInfo(subGoods.getId());
			if (tlist.size()>0){
				BigDecimal upMoney = BigDecimal.valueOf(price).subtract(BigDecimal.valueOf(tlist.get(0).getPrice()));
				BigDecimal up = upMoney.divide(BigDecimal.valueOf(tlist.get(0).getPrice()),2,BigDecimal.ROUND_HALF_DOWN);
				if (up.doubleValue() > 0.1 || up.doubleValue() < -0.1){
					aj.setSuccess(false);
					aj.setMsg("价格跌涨幅过大，请调整");
					return aj;
				}
			}else {
				BigDecimal upMoney = BigDecimal.valueOf(price).subtract(BigDecimal.valueOf(subGoods.getPrice()));
				BigDecimal up = upMoney.divide(BigDecimal.valueOf(subGoods.getPrice()),2,BigDecimal.ROUND_HALF_DOWN);
				if (up.doubleValue() > 0.1 || up.doubleValue() < -0.1){
					aj.setSuccess(false);
					aj.setMsg("价格跌涨幅过大，请调整");
					return aj;
				}
			}
			PositionAssets positionAssets = new PositionAssets();
			positionAssets.setMember(member);
			positionAssets.setSubGoods(subGoods);
			positionAssets.setIsApply("0");
			List<PositionAssets> assetsList = positionAssetsService.findList(positionAssets);
			if (assetsList.size()>0){
				SaleOrder s = new SaleOrder();
				s.setIsDeal("0");
				s.setIsRevoke("0");
				s.setMember(member);
				s.setSubGoods(subGoods);
				List<SaleOrder> list = findList(s);
				int sumOrder = list.stream().mapToInt(SaleOrder::getNonNum).sum();
				if (assetsList.get(0).getGoodsNum()-sumOrder >= num){
					//todo 判读余额是否充足
					Account account = accountService.get(member.getAccount().getId());
					if (account != null){
//						if (account.getYue() >= charge){
							//计算每单手续费

							SnowFlake snowFlake = new SnowFlake(2, 3);
							//生成订单编号
							Long orderFormNumber = snowFlake.nextId();
							String orderNo = orderFormNumber.toString();
							//新建买入订单
							SaleOrder saleOrder = new SaleOrder();
							saleOrder.setId(IdGen.uuid());
							saleOrder.setOrderNo(orderNo);
							saleOrder.setCharge(charge);
							saleOrder.setIsDeal("0");
							saleOrder.setIsRevoke("0");
							saleOrder.setMember(member);
							saleOrder.setNonNum(num);
							saleOrder.setNum(num);
							saleOrder.setPrice(price);
							saleOrder.setSubGoods(subGoods);
							saleOrder.setTotalPrice(totalPrice);
							saleOrder.preInsert();
							mapper.insert(saleOrder);
							//扣除手续费先
//							account.setYue(account.getYue()-charge);
//							accountService.save(account);
							//匹配卖出订单   卖出单价在此单价以下的
							PurchaseOrder purchaseOrder = new PurchaseOrder();
							purchaseOrder.setSubGoods(subGoods);
							purchaseOrder.setPrice(price);
							List<PurchaseOrder> purchaseOrderList = purchaseOrderService.findStandardPurchaseOrders(purchaseOrder);
							if (purchaseOrderList.size()>0){
								for (PurchaseOrder order : purchaseOrderList) {
									Member member1 = memberService.get(order.getMember().getId());
									double c = BigDecimal.valueOf(order.getPrice()*0.1).doubleValue();
									if (!order.getMember().getId().equals(member.getId())){
										//匹到的买入订单中 买入数量少
										if (order.getNonNum() < num){
											//成交数量
											Integer dealNum = order.getNonNum();
											//给买入用户加持仓
											PositionAssets ppAssets = new PositionAssets();
											ppAssets.setMember(order.getMember());
											ppAssets.setSubGoods(subGoods);
											ppAssets.setIsApply("0");
											List<PositionAssets> pp = positionAssetsService.findList(ppAssets);
											if (pp.size()>0){
												pp.get(0).setGoodsNum(pp.get(0).getGoodsNum()+(dealNum-order.getFreezingQuantity()));
												pp.get(0).preUpdate();
												positionAssetsMapper.update(pp.get(0));
											}else {
												PositionAssets positionAssets1 = new PositionAssets();
												positionAssets1.setId(IdGen.uuid());
												positionAssets1.setMember(order.getMember());
												positionAssets1.setSubGoods(subGoods);
												positionAssets1.setGoodsName(subGoods.getGoodsName());
												positionAssets1.setGoodsNum(dealNum-order.getFreezingQuantity());
												positionAssets1.setGoodsPrice(order.getPrice());
												positionAssets1.setTotalPrice(order.getPrice()*(dealNum-order.getFreezingQuantity()));
												positionAssets1.setIsApply("0");
												positionAssets1.setApplyNum(0);
												positionAssets1.preInsert();
												positionAssetsMapper.insert(positionAssets1);
											}
											//给买入的用户加锁仓
//											FreezingAssets freezingAssets = new FreezingAssets();
//											freezingAssets.setMember(order.getMember());
//											freezingAssets.setSubGoods(subGoods);
//											List<FreezingAssets> pf = freezingAssetsService.findList(freezingAssets);
//											if (pf.size()>0){
//												pf.get(0).setNum(pf.get(0).getNum()+order.getFreezingQuantity());
//												freezingAssetsService.save(pf.get(0));
//											}else {
//												FreezingAssets freezingAssets1 = new FreezingAssets();
//												freezingAssets1.setMember(order.getMember());
//												freezingAssets1.setSubGoods(subGoods);
//												freezingAssets1.setGoodsName(subGoods.getGoodsName());
//												freezingAssets1.setNum(order.getFreezingQuantity());
//												freezingAssets1.setGoodsPrice(order.getPrice());
//												freezingAssets1.setTotalPrice(order.getPrice()*order.getFreezingQuantity());
//												freezingAssetsService.save(freezingAssets1);
//											}
											order.setNonNum(0);
											order.setIsDeal("1");
											order.preUpdate();
											purchaseOrderMapper.update(order);
											//给买入的用户扣买入的钱
											Member purchaseMember = memberService.get(order.getMember().getId());
											Account purchaseAccount = accountService.get(purchaseMember.getAccount().getId());
											purchaseAccount.setYue(purchaseAccount.getYue()-(order.getPrice()*dealNum));
											purchaseAccount.preUpdate();
											accountMapper.update(purchaseAccount);

											//卖出订单扣除卖出数量
											saleOrder.setNonNum(saleOrder.getNonNum()-dealNum);
											saleOrder.preUpdate();
											mapper.update(saleOrder);
											//给卖出账户加钱
											account.setYue(account.getYue()+(order.getPrice()*dealNum)-(c*dealNum));
											account.preUpdate();
											accountMapper.update(account);
											//给卖出用户减去持仓
											PositionAssets sp = new PositionAssets();
											sp.setMember(member);
											sp.setSubGoods(subGoods);
											sp.setIsApply("0");
											List<PositionAssets> saleAssets = positionAssetsService.findList(sp);
											if (saleAssets.size()>0){
												saleAssets.get(0).setGoodsNum(saleAssets.get(0).getGoodsNum()-dealNum);
												saleAssets.get(0).preUpdate();
												positionAssetsMapper.update(saleAssets.get(0));
											}
											//成交记录
											TradeRecord tradeRecord = new TradeRecord();
											tradeRecord.setId(IdGen.uuid());
											tradeRecord.setSubGoods(subGoods);
											tradeRecord.setNum(dealNum);
											tradeRecord.setPrice(order.getPrice());
											tradeRecord.setPurchaseMember(order.getMember());
											tradeRecord.setPurchaseOrder(order.getOrderNo());
											tradeRecord.setSaleMember(member);
											tradeRecord.setTotalPrice(dealNum*order.getPrice());
											tradeRecord.setSaleOrderNo(orderNo);
											tradeRecord.preInsert();
											tradeRecordMapper.insert(tradeRecord);
											//给购买上级加业绩
//											purchaseOrderService.addManagersTeam(purchaseMember,dealNum*order.getPrice());
											//更新价格
											manageTradePriceForSale(subGoods,order);

											teamServiceService.manageTeamService(member1,c*dealNum,orderNo);

											if ("18888888888".equals(member1.getLoginName())){
												subGoodsService.addStock(subGoods,dealNum);
											}
											if ("18888888888".equals(member.getLoginName())){
												subGoodsService.substractStock(subGoods,dealNum);
											}
											num -= dealNum;
										}else if (order.getNonNum() == num){
											//成交数量
											Integer dealNum = order.getNonNum();
											//给买入用户加持仓
											PositionAssets ppAssets = new PositionAssets();
											ppAssets.setMember(order.getMember());
											ppAssets.setSubGoods(subGoods);
											ppAssets.setIsApply("0");
											List<PositionAssets> pp = positionAssetsService.findList(ppAssets);
											if (pp.size()>0){
												pp.get(0).setGoodsNum(pp.get(0).getGoodsNum()+(dealNum-order.getFreezingQuantity()));
												pp.get(0).preUpdate();
												positionAssetsMapper.update(pp.get(0));
											}else {
												PositionAssets positionAssets1 = new PositionAssets();
												positionAssets1.setId(IdGen.uuid());
												positionAssets1.setMember(order.getMember());
												positionAssets1.setSubGoods(subGoods);
												positionAssets1.setGoodsName(subGoods.getGoodsName());
												positionAssets1.setGoodsNum(dealNum-order.getFreezingQuantity());
												positionAssets1.setGoodsPrice(order.getPrice());
												positionAssets1.setTotalPrice(order.getPrice()*(dealNum-order.getFreezingQuantity()));
												positionAssets1.setIsApply("0");
												positionAssets1.setApplyNum(0);
												positionAssets1.preInsert();
												positionAssetsMapper.insert(positionAssets1);
											}
											//给买入的用户加锁仓
//											FreezingAssets freezingAssets = new FreezingAssets();
//											freezingAssets.setMember(order.getMember());
//											freezingAssets.setSubGoods(subGoods);
//											List<FreezingAssets> pf = freezingAssetsService.findList(freezingAssets);
//											if (pf.size()>0){
//												pf.get(0).setNum(pf.get(0).getNum()+order.getFreezingQuantity());
//												freezingAssetsService.save(pf.get(0));
//											}else {
//												FreezingAssets freezingAssets1 = new FreezingAssets();
//												freezingAssets1.setMember(order.getMember());
//												freezingAssets1.setSubGoods(subGoods);
//												freezingAssets1.setGoodsName(subGoods.getGoodsName());
//												freezingAssets1.setNum(order.getFreezingQuantity());
//												freezingAssets1.setGoodsPrice(order.getPrice());
//												freezingAssets1.setTotalPrice(order.getPrice()*order.getFreezingQuantity());
//												freezingAssetsService.save(freezingAssets1);
//											}
											order.setNonNum(0);
											order.setIsDeal("1");
											order.preUpdate();
											purchaseOrderMapper.update(order);
											//给买入的用户扣买入的钱
											Member purchaseMember = memberService.get(order.getMember().getId());
											Account purchaseAccount = accountService.get(purchaseMember.getAccount().getId());
											purchaseAccount.setYue(purchaseAccount.getYue()-(dealNum*order.getPrice()));
											purchaseAccount.preUpdate();
											accountMapper.update(purchaseAccount);
											//卖出订单扣除卖出数量
											saleOrder.setNonNum(0);
											saleOrder.setIsDeal("1");
											saleOrder.preUpdate();
											mapper.update(saleOrder);
											//给卖出账户+钱
											account.setYue(account.getYue()+(dealNum*order.getPrice())-(c*dealNum));
											account.preUpdate();
											accountMapper.update(account);
											//给卖出用户减去持仓
											PositionAssets sp = new PositionAssets();
											sp.setMember(member);
											sp.setSubGoods(subGoods);
											sp.setIsApply("0");
											List<PositionAssets> saleAssets = positionAssetsService.findList(sp);
											if (saleAssets.size()>0){
												saleAssets.get(0).setGoodsNum(saleAssets.get(0).getGoodsNum()-dealNum);
												saleAssets.get(0).preUpdate();
												positionAssetsMapper.update(saleAssets.get(0));
											}
											//成交记录
											TradeRecord tradeRecord = new TradeRecord();
											tradeRecord.setId(IdGen.uuid());
											tradeRecord.setSubGoods(subGoods);
											tradeRecord.setNum(dealNum);
											tradeRecord.setPrice(order.getPrice());
											tradeRecord.setPurchaseMember(order.getMember());
											tradeRecord.setPurchaseOrder(order.getOrderNo());
											tradeRecord.setSaleMember(member);
											tradeRecord.setSaleOrderNo(orderNo);
											tradeRecord.setTotalPrice(dealNum*order.getPrice());
											tradeRecord.preInsert();
											tradeRecordMapper.insert(tradeRecord);
											//给购买上级加业绩
//											purchaseOrderService.addManagersTeam(purchaseMember,dealNum*order.getPrice());
											//更新价格
											manageTradePriceForSale(subGoods,order);
											//处理手续费收益
											teamServiceService.manageTeamService(member1,c*dealNum,orderNo);

											if ("18888888888".equals(member1.getLoginName())){
												subGoodsService.addStock(subGoods,dealNum);
											}
											if ("18888888888".equals(member.getLoginName())){
												subGoodsService.substractStock(subGoods,dealNum);
											}
											break;
										}else if (order.getNonNum() > num){
											//成交数量
											Integer dealNum = num;
											if (dealNum>=order.getFreezingQuantity()){
												//给买入用户加持仓
												PositionAssets ppAssets = new PositionAssets();
												ppAssets.setMember(order.getMember());
												ppAssets.setSubGoods(subGoods);
												ppAssets.setIsApply("0");
												List<PositionAssets> pp = positionAssetsService.findList(ppAssets);
												if (pp.size()>0){
													pp.get(0).setGoodsNum(pp.get(0).getGoodsNum()+(dealNum-order.getFreezingQuantity()));
													pp.get(0).preUpdate();
													positionAssetsMapper.update(pp.get(0));
												}else {
													PositionAssets positionAssets1 = new PositionAssets();
													positionAssets1.setId(IdGen.uuid());
													positionAssets1.setMember(order.getMember());
													positionAssets1.setSubGoods(subGoods);
													positionAssets1.setGoodsName(subGoods.getGoodsName());
													positionAssets1.setGoodsNum(dealNum-order.getFreezingQuantity());
													positionAssets1.setGoodsPrice(order.getPrice());
													positionAssets1.setTotalPrice(order.getPrice()*(dealNum-order.getFreezingQuantity()));
													positionAssets1.setIsApply("0");
													positionAssets1.setApplyNum(0);
													positionAssets1.preInsert();
													positionAssetsMapper.insert(positionAssets1);
												}
												//给买入的用户加锁仓
//												FreezingAssets freezingAssets = new FreezingAssets();
//												freezingAssets.setMember(order.getMember());
//												freezingAssets.setSubGoods(subGoods);
//												List<FreezingAssets> pf = freezingAssetsService.findList(freezingAssets);
//												if (pf.size()>0){
//													pf.get(0).setNum(pf.get(0).getNum()+order.getFreezingQuantity());
//													freezingAssetsService.save(pf.get(0));
//												}else {
//													FreezingAssets freezingAssets1 = new FreezingAssets();
//													freezingAssets1.setMember(order.getMember());
//													freezingAssets1.setSubGoods(subGoods);
//													freezingAssets1.setGoodsName(subGoods.getGoodsName());
//													freezingAssets1.setNum(order.getFreezingQuantity());
//													freezingAssets1.setGoodsPrice(order.getPrice());
//													freezingAssets1.setTotalPrice(order.getPrice()*order.getFreezingQuantity());
//													freezingAssetsService.save(freezingAssets1);
//												}
											}
//											else {
//												//给买入的用户加锁仓
//												FreezingAssets freezingAssets = new FreezingAssets();
//												freezingAssets.setMember(order.getMember());
//												freezingAssets.setSubGoods(subGoods);
//												List<FreezingAssets> pf = freezingAssetsService.findList(freezingAssets);
//												if (pf.size()>0){
//													pf.get(0).setNum(pf.get(0).getNum()+(order.getFreezingQuantity()-dealNum));
//													freezingAssetsService.save(pf.get(0));
//												}else {
//													FreezingAssets freezingAssets1 = new FreezingAssets();
//													freezingAssets1.setMember(order.getMember());
//													freezingAssets1.setSubGoods(subGoods);
//													freezingAssets1.setGoodsName(subGoods.getGoodsName());
//													freezingAssets1.setNum(order.getFreezingQuantity()-dealNum);
//													freezingAssets1.setGoodsPrice(order.getPrice());
//													freezingAssets1.setTotalPrice(order.getPrice()*(order.getFreezingQuantity()-dealNum));
//													freezingAssetsService.save(freezingAssets1);
//												}
//											}
											order.setNonNum(order.getNonNum()-num);
//											order.setFreezingQuantity(order.getFreezingQuantity()-num);
											order.preUpdate();
											purchaseOrderMapper.update(order);
											//给买入的用户加扣买入的钱
											Member purchaseMember = memberService.get(order.getMember().getId());
											Account purchaseAccount = accountService.get(purchaseMember.getAccount().getId());
											purchaseAccount.setYue(purchaseAccount.getYue()-(order.getPrice()*num));
											purchaseAccount.preUpdate();
											accountMapper.update(purchaseAccount);
											//卖出订单扣除卖出数量
											saleOrder.setNonNum(0);
											saleOrder.setIsDeal("1");
											saleOrder.preUpdate();
											mapper.update(saleOrder);
											//给卖出账户+钱
											account.setYue(account.getYue()+(order.getPrice()*num)-(c*dealNum));
											account.preUpdate();
											accountMapper.update(account);
											//给卖出用户减去持仓
											PositionAssets sp = new PositionAssets();
											sp.setMember(member);
											sp.setSubGoods(subGoods);
											sp.setIsApply("0");
											List<PositionAssets> saleAssets = positionAssetsService.findList(sp);
											if (saleAssets.size()>0){
												saleAssets.get(0).setGoodsNum(saleAssets.get(0).getGoodsNum()-dealNum);
												saleAssets.get(0).preUpdate();
												positionAssetsMapper.update(saleAssets.get(0));
											}
											//成交记录
											TradeRecord tradeRecord = new TradeRecord();
											tradeRecord.setId(IdGen.uuid());
											tradeRecord.setSubGoods(subGoods);
											tradeRecord.setNum(dealNum);
											tradeRecord.setPrice(order.getPrice());
											tradeRecord.setPurchaseMember(order.getMember());
											tradeRecord.setPurchaseOrder(order.getOrderNo());
											tradeRecord.setSaleMember(member);
											tradeRecord.setSaleOrderNo(orderNo);
											tradeRecord.setTotalPrice(dealNum*order.getPrice());
											tradeRecord.preInsert();
											tradeRecordMapper.insert(tradeRecord);
											//给购买上级加业绩
//											purchaseOrderService.addManagersTeam(purchaseMember,dealNum*order.getPrice());
											//更新价格
											manageTradePriceForSale(subGoods,order);
											//处理手续费收益
											teamServiceService.manageTeamService(member1,c*dealNum,orderNo);

											if ("18888888888".equals(member1.getLoginName())){
												subGoodsService.addStock(subGoods,dealNum);
											}
											if ("18888888888".equals(member.getLoginName())){
												subGoodsService.substractStock(subGoods,dealNum);
											}
											break;
										}
									}
								}
								aj.setSuccess(true);
								aj.setMsg("卖出成功");
							}else {
								aj.setSuccess(true);
								aj.setMsg("操作成功");
							}
//							//处理手续费收益
//							teamServiceService.manageTeamService(member,charge,orderNo);
//						}else {
//							aj.setSuccess(false);
//							aj.setMsg("余额不足");
//						}

					}else {
						aj.setSuccess(false);
						aj.setMsg("账户信息有误");
					}
				}else {
					aj.setSuccess(false);
					aj.setMsg("商品持仓数量不足");
				}
			}else {
				aj.setSuccess(false);
				aj.setMsg("此商品暂无持仓有误");
			}

		}else {
			aj.setSuccess(false);
			aj.setMsg("商品信息有误");
		}

		return aj;
	}

	/**
	 * 撤销卖出订单
	 * @param member
	 * @param orderNo
	 * @return
	 */
	@Transactional(readOnly = false)
	public synchronized AjaxJson revokeSaleOrder(Member member, String orderNo) {
		AjaxJson aj = new AjaxJson();
		SaleOrder saleOrder = new SaleOrder();
		saleOrder.setMember(member);
		saleOrder.setOrderNo(orderNo);
		List<SaleOrder> list = findList(saleOrder);
		if (list.size()>0){
			for (SaleOrder order : list) {
				order.setIsRevoke("1");
				save(order);
				//每单手续费
//				double c = BigDecimal.valueOf(order.getCharge()).divide(BigDecimal.valueOf(order.getNum()),2,BigDecimal.ROUND_HALF_DOWN).doubleValue();
//				//剩余手续费返还
//				double charge = c * order.getNonNum();
//				Member m = memberService.get(order.getMember().getId());
//				if (m != null){
//					Account account = accountService.get(m.getAccount().getId());
//					if (account != null){
//						account.setYue(account.getYue() + charge);
//						account.preUpdate();
//						accountMapper.update(account);
//					}
//				}
			}
		}
		aj.setSuccess(true);
		aj.setMsg("撤销成功");
		return aj;
	}

	@Transactional(readOnly = false)
	public void manageTradePriceForSale(SubGoods subGoods,PurchaseOrder order){
		//所有此商品的持仓 改动现价
		PositionAssets pAssets = new PositionAssets();
		pAssets.setSubGoods(subGoods);
		pAssets.setIsApply("0");
		List<PositionAssets> assetsList1 = positionAssetsService.findList(pAssets);
		if (assetsList1.size()>0){
			for (PositionAssets assets : assetsList1) {
				if (assets.getGoodsPrice() != 0){
					assets.setNowPrice(order.getPrice());
					assets.setNowTotal(assets.getNowPrice()*assets.getGoodsNum());
					assets.setUpPrice(order.getPrice()-assets.getGoodsPrice());
					assets.setUpPercentage(BigDecimal.valueOf(assets.getUpPrice())
							.divide(BigDecimal.valueOf(assets.getGoodsPrice()),2,BigDecimal.ROUND_HALF_DOWN)
							.multiply(BigDecimal.valueOf(100)).doubleValue());
					positionAssetsService.save(assets);
				}
			}
		}
		//所有此商品的锁仓 改动现价
		FreezingAssets freezingAssets = new FreezingAssets();
		freezingAssets.setSubGoods(subGoods);
		freezingAssets.setIsApply("0");
		List<FreezingAssets> assetsList2 = freezingAssetsService.findList(freezingAssets);
		if (assetsList2.size()>0){
			for (FreezingAssets assets : assetsList2) {
				assets.setNowPrice(order.getPrice());
				assets.setNowTotal(assets.getNowPrice()*assets.getNum());
				freezingAssetsService.save(assets);
			}
		}
		//如果成交价到了原价3倍以上
//		if (BigDecimal.valueOf(order.getPrice()).divide(BigDecimal.valueOf(subGoods.getPrice()),0,BigDecimal.ROUND_DOWN).compareTo(BigDecimal.valueOf(3)) == 0
//				||BigDecimal.valueOf(order.getPrice()).divide(BigDecimal.valueOf(subGoods.getPrice()),0,BigDecimal.ROUND_DOWN).compareTo(BigDecimal.valueOf(3)) == 1){
//			subGoodsService.handleInvestment(subGoods);
//		}
//										Member purchaseMember = memberService.get(order.getMember().getId());
		//处理用户等级
//										memberService.manageMemberType(purchaseMember);
	}

	public List<SaleOrder> findLimitList(SaleOrder saleOrder) {
		return mapper.findLimitList(saleOrder);
	}

	public void revokeOrders() {
		mapper.revokeOrders();
	}

	public List<SaleOrder> findLimitByPriceList(SaleOrder saleOrder) {
		return mapper.findLimitByPriceList(saleOrder);
	}

	public List<SaleOrder> findListAll(SaleOrder saleOrder) {
		return mapper.findList(saleOrder);
	}
}